
var Report = {
	subSys:'',
	repID:0,
	pageRes:null,
	fields:null,
	cnds:null,
	dicsMap:null,
	funcs:null,
	grid:null,
	
	init:function(){
		if(reportUrl==""){
			System.showError('错误', "系统错误：报表必须提供URL!");
			top.$.messager.progress('close');
			return;
		}
		if(!Report.pageInfo()){
			top.$.messager.progress('close');
			return;
		}
		/*
		var subSys = System.getRequest("subSys",reportUrl);
		var repID = System.getRequest("repID",reportUrl);
		top.$.messager.progress('close');
		if(!subSys||!repID){
			System.showError('错误', "报表URL中必须提供subSys及repID参数值!");
			return;
		}
		Report.subSys = subSys;
		Report.repID = repID;
		 */
		var isShowCnd = System.getRequest("isShowCnd",reportUrl);
		if(isShowCnd&&isShowCnd==0){
		}else{
			Report.initCnd();
		}
		var dataUrl = System.getRootPath()+"/"+reportUrl;
		Report.funcToMap();
		var toolbar = Grid.createToolBar(Report.funcs, Report.dealFunc);
		var gridOpts = {
			url:dataUrl,
			toolbar:toolbar,
			columns:Report.fields
		}
		if(Report.pageRes.isEdit==1){
			gridOpts.isEdit=1;
		}
		Grid.dicsMap = Report.dicsMap;
		var _grid = Grid.initGrid(gridOpts,"grid");
		Report.grid = _grid;
		top.$.messager.progress('close');
//		$('#openOrClose').on("click",function(){
//			$('#conditon').toggle(80);
//			setTimeout(domresize(),100);
//		});
//		setTimeout(domresize(),300);
	},
	pageInfo:function(){
		var url = System.getRootPath()+"/"+reportUrl;
		var result = false;
		System.ajaxGet(url,function(data){
			Report.pageRes = data.repRes;
			Report.fields = data.fields;
			Report.cnds = data.cnds;
			Report.dicsMap = data.dicsMap;
			Report.funcs = data.funcs;
			result = true;
		},false);
		return result;
	},
	initCnd:function(){
		if(Report.cnds.length<1){
//			$("#search_area").empty();
			return;
		}
		$("#search_area").show();
		Form.formId = 'searchForm';
		Form.formType = 'search';
		var formTable = {};
		formTable.ftType = 1;
		var arrayFmt = "";
		$.each(Report.cnds,function(i,cnd){
			var cndName = cnd.tagName;
			if(i%2==0){
				arrayFmt = arrayFmt + ";" + cndName;
			}else{
				arrayFmt = arrayFmt + "," + cndName;
			}
		});
		formTable.arrayFmt = arrayFmt;
		formTable.elemTagList = Report.cnds;
		formTable.dicsMap = Report.dicsMap;
		var formTables = new Array(1);
		formTables[0] = formTable;
		Form.formTables = formTables;
		Form.resObj = Report;
		Form.init();
	},
	dealFunc:function(funcId){
		var func = Report.funcMap[funcId];
		if(func.funcType==1){
			Report.add(func);
		}else if(func.funcType==2){
			Report.edit(func);
		}else if(func.funcType==3){
			Report.del(func);
		}else if(func.funcType==5){//批量保存
			Report.batchUpd(func);
		}else if(func.funcType==11){//返回
			Report.handleBack();
		}
	},
	funcToMap:function(){
		var funcMap = {};
		$.each(Report.funcs,function(i,func){
			funcId = func.funcId;
			funcMap[funcId] = func;
		});
		Report.funcMap = funcMap;
	},
	add:function(func){
		top.$.messager.progress();
		var funcUrl = func.funcUrl;
		var pageUrl = System.getPageUrl(funcUrl);
		if(!pageUrl){
			return;
		}
		var _width = 500;
		var _height = 380;
		var href = pageUrl+'?funcUrl='+encodeURIComponent(funcUrl);
		var dlgId = System.openDialog(func.dispName,href,_width,_height,this);
		return;
		/*
		if(parent.Report&&parent.Report.dialog){
			_width = 400;
			_height = 300;
		}
		Report.dialog = $('#Dlg').dialog({
			title:func.dispName,
	        closed: false,
	        modal: true,
	        width: _width,    
	        height: _height,
	        href:pageUrl+'?funcUrl='+encodeURIComponent(funcUrl),
	        isIframe:true
	    });
	    */
	},
	edit:function(func){
		var funcUrl = func.funcUrl;
		var urlParams = func.urlParams;
		var rows = Report.grid.datagrid('getSelections');
	    var num = rows.length;
	    if (num == 0) {
	    	$.messager.alert('错误', "请选择一条记录进行操作!", 'error');
	        return;
	    }else if (num > 1) {
	    	$.messager.alert('错误', "您选择了多条记录,只能选择一条记录进行修改!", 'error');
	        return;
	    }else{
	    	if(urlParams){
	    		var urlParamArray = [];
	    		urlParams = urlParams.split(",");
	    		$.each(urlParams,function(i,urlParam){
	    			var urlParamvalue = rows[0][urlParam];
	    			urlParamArray.push(urlParamvalue);
	    		});
	    		funcUrl = ToolsUtil.strReplaceData(funcUrl,"\\|\\*\\|",urlParamArray);
	    	}
	    	var pageUrl = System.getPageUrl(funcUrl);
			if(!pageUrl){
				return;
			}
	    	top.$.messager.progress();
	    	var href = pageUrl+'?funcUrl='+encodeURIComponent(funcUrl);
	    	var dlgId = System.openDialog(func.dispName,href,500,380);
	    	/*
	    	Report.dialog = $('#Dlg').dialog({
	    		title:func.dispName,
	    		closed: false,
	    		modal: true,
	    		width: 500,    
	    		height: 380,
	    		href:pageUrl+'?funcUrl='+encodeURIComponent(funcUrl),
	    		isIframe:true
	    	});
	    	*/
	    }
	},
	del:function(func) {
		var funcUrl = func.funcUrl;
		if(funcUrl==""){
			System.showError('错误', "系统错误：删除功能remove的类型为用户自定义，必须提供功能URL!");
			top.$.messager.progress('close');
			return;
		}
		var urlParams = func.urlParams;
		var rows = Report.grid.datagrid('getSelections');
	    var num = rows.length;
	    if (num ==1) {
	        $.messager.confirm('提示信息', '您确认要删除吗?', function (data) {
	            if (data) {
	            	if(urlParams){
	    	    		var urlParamArray = [];
	    	    		urlParams = urlParams.split(",");
	    	    		$.each(urlParams,function(i,urlParam){
	    	    			var urlParamvalue = rows[0][urlParam];
	    	    			urlParamArray.push(urlParamvalue);
	    	    		});
	    	    		funcUrl = ToolsUtil.strReplaceData(funcUrl,"\\|\\*\\|",urlParamArray);
	    	    	}
	            	Report.doDel(funcUrl);
	            }
	        });
	    } else if(num!=1){
	    	System.showMsg('请先选择一条要删除的记录。');
	    }
	},
	doDel:function(funcUrl){
		System.ajaxGet(funcUrl,function(data){
			Report.grid.datagrid('reload');
			Report.grid.datagrid('clearSelections');
			/*
			if (data.result) {
			} else {
				System.showMsg('删除失败!','错误','error');
			}
			*/
		});
	},
	batchUpd:function(func){
		var data = {};
		var trs = $("div.datagrid-view div.datagrid-view2 table.datagrid-btable").find("tr.datagrid-row");
		var size = trs.length;
		for(var index=0;index<size;index++){
			var tr = trs[index];
			var inputs = $(tr).find("input");
			$.each(inputs,function(i,input){
				var fieldName = $(input).attr("name");
				var fieldValue = $(input).attr("value");
				if(fieldName!=undefined){
					var idFields = data[fieldName];
					if(!idFields){
						idFields = fieldValue;
					}else{
						idFields = idFields+","+fieldValue;
					}
					data[fieldName] = idFields;
				}
			});
			var editors = Report.grid.datagrid('getEditors',index);
			$.each(editors,function(i,editor){
				var field = editor.field;
				var value = editor.target.val();
				var fields = data[field];
				if(!fields){
					fields = value;
				}else{
					fields = fields+","+value;
				}
				data[field] = fields;
			});
		}
		/*
		var rows = Report.grid.datagrid('getRows');
		var idField = Report.grid.datagrid('options').idField;
		size = rows.length;
		
		var idFields = new Array();
		if(idField){
			idFields = idField.split(",");
		}
		for(var index=0;index<size;index++){
			var row = rows[index];
			console.info(row);
			$.each(idFields,function(i,id){
				var idvalue = row[id];
				var idFields = data[id];
				if(!idFields){
					idFields = idvalue;
				}else{
					idFields = idFields+","+idvalue;
				}
				data[id] = idFields;
			});
			var editors = Report.grid.datagrid('getEditors',index);
			$.each(editors,function(i,editor){
				var field = editor.field;
				var value = editor.target.val();
				var fields = data[field];
				if(!fields){
					fields = value;
				}else{
					fields = fields+","+value;
				}
				data[field] = fields;
			});
//			Report.grid.datagrid('endEdit',index);
		}
		*/
		var batchUpdUrl = System.getRootPath()+'/sysmng.batchUpd.do?subSys='+Report.pageRes.subSys+"&repID="+Report.pageRes.repId;
		var postData = "recordNum="+size;
		for(var fieldName in data){
			if(!postData){
				postData = fieldName+"="+data[fieldName];
			}else{
				postData = postData+"&"+fieldName+"="+data[fieldName];
			}
		}
		System.ajaxPost(batchUpdUrl,postData,function(data){
			System.showError("提示", "批量修改成功！");
		});
//		rows = Report.grid.datagrid('getRows');
//		console.info(rows);
	},
	handleBack:function(){
		history.back();
	},
	search:function(){
		var cndJson = Form.getFormData();
		Report.grid.datagrid('load',cndJson);
	},
	domresize:function(){
		$('#grid').datagrid('resize',{  
			height:$("#body").height()-$('#search_area').height()-5,
			width:$("#body").width()
		});
	},
	getSelectedArr:function() {
	    var ids = [];
	    var selects = Report.grid.datagrid("getSelections");
	    for (var i = 0; i < selects.length; i++) {
	        ids.push(selects[i].DBRESID);
	    }
	    return ids;
	},
	handleLink:function(linkURL,target,queryInfo){//queryInfo 询问信息.
		var pageUrl = System.getPageUrl(linkURL);
		if(!pageUrl){
			return;
		}
		if(ToolsUtil.isNotNull(queryInfo)){
			if(!confirm(queryInfo)){
				return;
			}
		}
		if(pageUrl=='ajax'){
//			Report.doDel(linkURL);
			System.ajaxGet(linkURL,function(data){
				Report.grid.datagrid('reload');
				Report.grid.datagrid('clearSelections');
			});
		}else{
			var url = pageUrl+'?funcUrl='+encodeURIComponent(linkURL);
			if(!ToolsUtil.isNotNull(target)){
				target="_pop";
			}
			if(target=='_pop'){//弹窗
				System.openDialog(' ',url,500,380,this);
				/*
				Report.dialog = $('#Dlg').dialog({
					title:' ',
					closed: false,
					modal: true,
					width: 500,
					height: 380,
					href:url,
					isIframe:true
				})
				*/
			}else if(target=='_self'){
				System.redirect(linkURL);
			}
		}
	},
	fireSetPopDic:function(){
		if(!popInputId){
			return;
		}
		if(!pDlgId){
			pDlgId = "";
		}
		PopDic.setPopDic(popInputId,pDlgId,true,',');
	}
}

